﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rotempco.BaseClass;
using Rotempco.BaseClass.Objects.AttributeStoreObjects;
using Rotempco.BaseClass.Objects.Enums;

namespace Rotempco.Website.WebPages.Admin.ChosenLanguages
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DataBindControls();
        }

        private void DataBindControls()
        {
            ActiveLanguages activeLanguages = Factory.ActiveLanguages.GetActiveLanguages();

            foreach (string langauge in Enum.GetNames(typeof(LanguageTypeEnum)))
            {
                LanguageTypeEnum languageTypeEnum = (LanguageTypeEnum)Enum.Parse(typeof(LanguageTypeEnum), langauge);
                if (languageTypeEnum == LanguageTypeEnum.NA)
                    continue;

                ListItem listItem = new ListItem(languageTypeEnum.ToString(), ((int)languageTypeEnum).ToString());
                listItem.Selected = (from a in activeLanguages.ActiveLangaugeList where a == languageTypeEnum select a).Count() > 0;
                CheckBoxList_ActiveLanguages.Items.Add(listItem);
            }
        }

        protected bool button_Save()
        {
            ActiveLanguages activeLanguages = Factory.ActiveLanguages.GetActiveLanguages();

            activeLanguages.ActiveLangaugeList.Clear();
            foreach (ListItem listItem in CheckBoxList_ActiveLanguages.Items)
            {
                if (listItem.Selected)
                    activeLanguages.ActiveLangaugeList.Add((LanguageTypeEnum)int.Parse(listItem.Value));
            }

            Factory.ActiveLanguages.SetActiveLanguages(activeLanguages);

            Response.Redirect(Request.Url.OriginalString);

            return false;
        }        
    }
}